<?php
/**
 * Created by PhpStorm.
 * User: costa92
 * Date: 2016/11/7
 * Time: 下午3:49
 */
namespace App\Libraries\classs;
use App\Models\System;
use Illuminate\Support\Facades\Redis as Redis;
use App\Models\WxUser;
use App\Models\Buildings;
use App\Models\Build\Star\Ranks as Ranks;
use App\Models\User\Vote\Logs as Logs;
use Symfony\Component\VarDumper\Cloner\Data;

class PhpRedis
{
    protected  $_valid_time =600;
    public function __construct(){

    }

    static $instance;
    public static function  getInstance(){
        if( self::$instance==null ){
            self::$instance = new self();
        }
        return self::$instance;
    }

    // 根据openid 获取用户为
    public function getRedisUser($user_date){
             $user = Redis::get($user_date["openid"]);
            if(!$user){
                $user = WxUser::where(array('openid'=>$user_date["openid"]))->first();
                if(empty($user->id)){
                    unset($user_date['privilege']);
                    $user_date['addtime']=time();
                    $user= WxUser::create($user_date);
                    $user->username ="";
                    $user->phone ="";
                }else{
                    if(!$user->nickname){
                        $user->nickname=$user_date['nickname'];
                        $user->update();
                    }
                }
                Redis::setex($user_date["openid"],$this->_valid_time,$user) ;
            }else{
                $user= json_decode($user);
            }
            return $user;
    }

    // 更新redis用户信息
     public function  updateRedisUser($uid,$data){
         if($uid && $data){
              $user = WxUser::find($uid);
              if($user){
                    $user->username = $data['username'];
                    $user->phone = $data['phone'];
                    $user->save();
                    Redis::setex($user->openid,$this->_valid_time,$user);
                    return true;
              }
         }
         return false;
     }

    //  获取总人数
  public function  getRedisUserCount(){
      $user_count = Redis::get("user_count");
      if(!$user_count){
          $user_count = WxUser::count();
          $system = System::find(1);
          $user_count = $user_count + $system->user_count;
          Redis::setex('user_count',$this->_valid_time,$user_count) ;
      }
      return $user_count;
  }

  //获取总星数
    public function getRedisBuildStarSum(){
        $build_star_sum = Redis::get("build_star_sum");
        if(!$build_star_sum){
            $build_star_sum=Buildings::sum('vote')?Buildings::sum('vote'):0;
            $build_star_sum_count=Buildings::sum('vote_count')?Buildings::sum('vote_count'):0; //假数据
            $build_star_sum =$build_star_sum+$build_star_sum_count;
            Redis::setex('build_star_sum',$this->_valid_time,$build_star_sum) ;
        }
        return $build_star_sum;
    }

    //获取总人气
    public function getRedisBuildCountSum(){
        $build_count = Redis::get("build_count_sum");
        if(!$build_count){
            $build_count = Buildings::sum('count')?Buildings::sum('count'):0;
            Redis::setex('build_count_sum',$this->_valid_time,$build_count) ;
        }
        return $build_count;
    }

  //七星排行
    public function getRedisStarRank(){
        $build_star_rank = Redis::get("build_star_rank");
        if(!$build_star_rank){
            $this->getCalculate();
            $build_star_rank_list=Ranks::orderBy('rank','asc')->get();
            if($build_star_rank_list){
                $data = array();
                foreach ($build_star_rank_list as $row){
                    $build=Buildings::with(array('attach'))->select('name','vote_count','info','vote','count','id','attach_id')->find($row->build_id);
                    $build->star=$row->stas+$build->vote_count;
                    $data[]=$build->toArray();
                }
                Redis::setex('build_star_rank',$this->_valid_time,json_encode($data)) ;
                $build_star_rank = $data;
            }
        }else{
            $build_star_rank = json_decode($build_star_rank);
        }
       return $build_star_rank;
    }

    //人气排行
    public function getRedisCountRank(){
        $build_count_rank = Redis::get("build_count_rank");
        if(!$build_count_rank){
            $build_count_rank = Buildings::with('attach')->where('valid','=',1)->orderBy('count','desc')->get(array('name','vote_count','info','vote','count','id','attach_id'));
            if($build_count_rank){
                foreach ($build_count_rank as $row){
                    $ranks = Ranks::where('build_id','=',$row->id)->first();
                    $row->star =$ranks?$ranks->stas+$row->vote_count:$row->vote_count;
                }
                Redis::setex('build_count_rank',$this->_valid_time,$build_count_rank) ;
                $build_count_rank = $build_count_rank->toArray();
            }
        }else{
            $build_count_rank = json_decode($build_count_rank);
        }
        return $build_count_rank;
    }

    //投票记录滚动
    public function getVoteLog(){
        $vote_log = Redis::get("vote_log");
        if(!$vote_log){
            $vote_log =Logs::with(array('users'=>function($query){
                $query->select('id','nickname','headimgurl');
            },'build'=>function($query){
                $query->select('id','name');
            }))->take(5)->select('id','x_uid','build_id','count')->orderBy('id','desc')->get();
            if($vote_log){
                Redis::setex('vote_log',$this->_valid_time,$vote_log) ;
                $vote_log= $vote_log->toArray();
            }
        }else{
            $vote_log = json_decode($vote_log);
        }
        return $vote_log;
    }

    //  计算楼盘七星值
    public function getCalculate(){
        $build = Buildings::where('valid','=',1)->orderBy('vote','desc')->get();
        if($build){
            Ranks::truncate();
            $i=1;
            foreach ($build as $row){
                $rank_data=array(
                    'build_id'=>$row->id,
                    'stas'=>floor($row->vote/7),
                    'rank'=>$i,
                    'addtime'=>time(),
                );
               if(Ranks::create($rank_data)){
                   $row->vote_star= floor($row->vote/7);
                   $row->save();
                   $i++;
               }
            }
        }
    }
}